package com.ruoyi.interfaceConfig.sceneConfig.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.interfaceConfig.businessObject.domain.BusinessFieldSetting;
import com.ruoyi.interfaceConfig.businessObject.domain.BusinessObjectConfig;
import com.ruoyi.interfaceConfig.businessObject.mapper.BusinessFieldSettingMapper;
import com.ruoyi.interfaceConfig.businessObject.mapper.BusinessObjectConfigMapper;
import com.ruoyi.interfaceConfig.sceneConfig.domain.dto.SceneConfigDTO;
import com.ruoyi.interfaceConfig.sceneConfig.domain.vo.FieldMappingVo;
import com.ruoyi.interfaceConfig.sceneConfig.domain.vo.SceneConfigVo;
import com.ruoyi.interfaceConfig.sceneConfig.domain.vo.SceneSearchParams;
import com.ruoyi.interfaceConfig.sceneConfig.mapper.FieldMappingMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.interfaceConfig.sceneConfig.mapper.SceneConfigMapper;
import com.ruoyi.interfaceConfig.sceneConfig.domain.SceneConfig;
import com.ruoyi.interfaceConfig.sceneConfig.service.ISceneConfigService;

/**
 * 集成场景配置Service业务层处理
 * 
 * @author YiYuan
 * @date 2024-02-06
 */
@Service
@RequiredArgsConstructor
public class SceneConfigServiceImpl implements ISceneConfigService 
{
    private final SceneConfigMapper sceneConfigMapper;
    private final BusinessObjectConfigMapper businessObjectConfigMapper;
    private final FieldMappingMapper fieldMappingMapper;

    /**
     * 查询集成场景配置
     * 
     * @param id 集成场景配置主键
     * @return 集成场景配置
     */
    @Override
    public SceneConfigVo selectSceneConfigById(Long id)
    {
        return sceneConfigMapper.selectSceneConfigById(id);
    }

    /**
     * 查询集成场景配置列表
     * 
     * @param sceneConfig 集成场景配置
     * @return 集成场景配置
     */
    @Override
    public List<SceneConfigVo> selectSceneConfigList(SceneConfig sceneConfig)
    {
        return sceneConfigMapper.selectSceneConfigList(sceneConfig);
    }

    /**
     * 新增集成场景配置
     * 
     * @param sceneConfig 集成场景配置
     * @return 结果
     */
    @Override
    public Long insertSceneConfig(SceneConfig sceneConfig)
    {
        //根据系统1对象找到所属系统，确定同步方向
        BusinessObjectConfig businessObjectConfig = businessObjectConfigMapper.selectBusinessObjectConfigById(sceneConfig.getSysObjId1());
        if(StringUtils.equals("01", businessObjectConfig.getSysPlatform())) {
            //序列号追溯系统，同步方向，本系统 ===》 金蝶
            sceneConfig.setIntegrationDirection("01");
        }else{
            //从左到右
            sceneConfig.setIntegrationDirection("02");
        }
        sceneConfigMapper.insertSceneConfig(sceneConfig);
        return sceneConfig.getId();
    }

    /**
     * 修改集成场景配置
     * 
     * @param sceneConfig 集成场景配置
     * @return 结果
     */
    @Override
    public int updateSceneConfig(SceneConfig sceneConfig)
    {
        return sceneConfigMapper.updateSceneConfig(sceneConfig);
    }

    /**
     * 批量删除集成场景配置
     * 
     * @param ids 需要删除的集成场景配置主键
     * @return 结果
     */
    @Override
    public int deleteSceneConfigByIds(Long[] ids)
    {
        return sceneConfigMapper.deleteSceneConfigByIds(ids);
    }

    /**
     * 删除集成场景配置信息
     * 
     * @param id 集成场景配置主键
     * @return 结果
     */
    @Override
    public int deleteSceneConfigById(Long id)
    {
        return sceneConfigMapper.deleteSceneConfigById(id);
    }

    @Override
    public Map<String, Object> getSceneConfigDetail(Long id) {
        SceneConfigVo sceneConfig = selectSceneConfigById(id);
        String integrationDirection = sceneConfig.getIntegrationDirection();
        //integrationDirection为 02表示从左到右，即右边为目标字段
        Long mainBusinessId = null;
        Long otherBusinessId = null;
        if("02".equals(integrationDirection)) {
            mainBusinessId = sceneConfig.getSysObjId2();
            otherBusinessId = sceneConfig.getSysObjId1();
        }else {
            mainBusinessId = sceneConfig.getSysObjId1();
            otherBusinessId = sceneConfig.getSysObjId2();

        }
        //根据主ID查询相应字段信息

        //查询映射的字段信息
        List<FieldMappingVo> fieldMappingVos =  fieldMappingMapper.selectFieldMappingBySceneId(id, mainBusinessId, otherBusinessId);
        Map<String,Object> map = new HashMap<>(3, 1f);
        map.put("sceneConfig", sceneConfig);
        map.put("fieldMappingVos", fieldMappingVos);
        return map;
    }

    @Override
    public List<SceneConfigDTO> getSceneSearchList(SceneSearchParams sceneSearchParams) {
        return sceneConfigMapper.getSceneSearchList(sceneSearchParams);
    }

    @Override
    public List<Long> getSceneConfigIds(String businessObjectFlag) {
        return sceneConfigMapper.getSceneConfigIds(businessObjectFlag);
    }
}
